package com.ruoyi.answer.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.answer.domain.vo.ExamAnswerSheetVo;
import com.ruoyi.answer.domain.bo.ExamAnswerSheetBo;
import com.ruoyi.answer.service.IExamAnswerSheetService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 试卷答题记录
 *
 * @author ruoyi
 * @date 2022-08-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/answer/sheet")
public class ExamAnswerSheetController extends BaseController {

    private final IExamAnswerSheetService iExamAnswerSheetService;

    /**
     * 查询试卷答题记录列表
     */
    @SaCheckPermission("answer:sheet:list")
    @GetMapping("/list")
    public TableDataInfo<ExamAnswerSheetVo> list(ExamAnswerSheetBo bo, PageQuery pageQuery) {
        return iExamAnswerSheetService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出试卷答题记录列表
     */
    @SaCheckPermission("answer:sheet:export")
    @Log(title = "试卷答题记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ExamAnswerSheetBo bo, HttpServletResponse response) {
        List<ExamAnswerSheetVo> list = iExamAnswerSheetService.queryList(bo);
        ExcelUtil.exportExcel(list, "试卷答题记录", ExamAnswerSheetVo.class, response);
    }

    /**
     * 获取试卷答题记录详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("answer:sheet:query")
    @GetMapping("/{id}")
    public R<ExamAnswerSheetVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iExamAnswerSheetService.queryById(id));
    }

    /**
     * 新增试卷答题记录
     */
    @SaCheckPermission("answer:sheet:add")
    @Log(title = "试卷答题记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ExamAnswerSheetBo bo) {
        return toAjax(iExamAnswerSheetService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改试卷答题记录
     */
    @SaCheckPermission("answer:sheet:edit")
    @Log(title = "试卷答题记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ExamAnswerSheetBo bo) {
        return toAjax(iExamAnswerSheetService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除试卷答题记录
     *
     * @param ids 主键串
     */
    @SaCheckPermission("answer:sheet:remove")
    @Log(title = "试卷答题记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iExamAnswerSheetService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
